package cn.myeasyai.config;

import cn.myeasyai.core.face.Face;
import cn.myeasyai.util.FaceFactory;
import cn.myeasyai.util.IdWorker;
import com.obs.services.ObsClient;
import io.milvus.client.MilvusServiceClient;
import io.milvus.param.ConnectParam;
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.common.DataType;
import io.milvus.v2.service.collection.request.AddFieldReq;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;


/**
 * @author fushoujiang
 */
@Configuration
public class SpringConfig {
    @Bean
    @ConditionalOnProperty(name = "file.model", havingValue = "obs")
    public ObsClient obsClient(FileConfig FileConfig){
        final FileConfig.Obs config = FileConfig.getObs();
        return  new ObsClient(config.getAccessKeyId(), config.getSecretAccessKeyId(),config.getEndPoint());
    }

    @Bean
    public MilvusServiceClient milvusServiceClient(MilvusConfig milvusConfig){
        return new MilvusServiceClient (ConnectParam.newBuilder()
                .withUri(milvusConfig.getUri())
                .build());
    }
    @Bean
    public MilvusClientV2 MilvusClientV2Client(MilvusConfig milvusConfig){
        MilvusClientV2 milvusClientV2 = new MilvusClientV2(
                ConnectConfig.builder()
                        .uri(milvusConfig.getUri()).build());

        CreateCollectionReq.CollectionSchema schema = milvusClientV2.createSchema();
        // 3.2 Add fields to schema
        schema.addField(AddFieldReq.builder()
                .fieldName("Auto_id")
                .dataType(DataType.Int64)
                .isPrimaryKey(true)
                .autoID(true)
                .build());

        schema.addField(AddFieldReq.builder()
                .fieldName("vector")
                .dataType(DataType.FloatVector)
                .dimension(2226)
                .build());

        schema.addField(AddFieldReq.builder()
                .fieldName("face_url")
                .dataType(DataType.VarChar)
                .maxLength(1000)
                .build());

        schema.addField(AddFieldReq.builder()
                .fieldName("face_feature_url")
                .dataType(DataType.VarChar)
                .maxLength(1000)
                .build());


        schema.addField(AddFieldReq.builder()
                .fieldName("user_id")
                .dataType(DataType.Int64)
                .build());

        schema.addField(AddFieldReq.builder()
                .fieldName("user_name")
                .dataType(DataType.VarChar)
                .maxLength(1000)
                .build());

        CreateCollectionReq customizedSetupReq2 = CreateCollectionReq.builder()
                .collectionName(milvusConfig.getCollectionName())
                .collectionSchema(schema)
                .build();

        milvusClientV2.createCollection(customizedSetupReq2);


        return milvusClientV2;
    }

    @Bean
    public IdWorker idWorker(IdWorkConfig workConfig){
        return new IdWorker(workConfig.getWorkerId(),workConfig.getDateCenterId());
    }
    @Bean
    public Face face(FaceConfig faceConfig ){

        if (StringUtils.isNotBlank(faceConfig.getAvgFace()) && StringUtils.isNotBlank(faceConfig.getFaceModel())){
            return FaceFactory.getFace(faceConfig.getAvgFace(), faceConfig.getFaceModel());

        }
        return FaceFactory.getFace();


    }
}
